package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.ScoreLotteryRecordDao;
import com.qdlc.p2p.dal.dto.ScoreLotteryRecord;
import com.qdlc.p2p.dal.model.ScoreLotteryModel;
import com.qdlc.p2p.dal.model.ScoreLotteryRecordModel;

@Repository("scoreLotteryRecordDao")
public class ScoreLotteryRecordDaoImpl extends BaseDaoImpl<ScoreLotteryRecord> implements ScoreLotteryRecordDao {

    @Override
    public PageDataList<ScoreLotteryRecordModel> findPageList(ScoreLotteryRecordModel model) {
        QueryParam param = QueryParam.getInstance();
        if (StringUtil.isNotBlank(model.getSearchName())) {//模糊查询条件
            param.addParam("user.userName", Operators.LIKE, model.getSearchName().trim());
        } else {
            if (StringUtil.isNotBlank(model.getUserName())) {
                param.addParam("user.userName", Operators.LIKE, model.getUserName().trim());
            }
            if (StringUtil.isNotBlank(model.getRealName())) {
                param.addParam("user.realName", Operators.LIKE, model.getRealName().trim());
            }
            if (StringUtil.isNotBlank(model.getStartTime())) {
                Date start = DateUtil.valueOf(model.getStartTime() + " 00:00:00");
                param.addParam("addTime", Operators.GTE, start);
            }
            if (StringUtil.isNotBlank(model.getEndTime())) {
                Date end = DateUtil.valueOf(model.getEndTime() + " 23:59:59");
                param.addParam("addTime", Operators.LTE, end);
            }
            if (model.getType() != 99) {
                param.addParam("type", model.getType());
            }
        }
        param.addPage(model.getPage(), model.getRows());
        param.addOrder(OrderType.DESC, "id");
        PageDataList<ScoreLotteryRecord> pageDataList = super.findPageList(param);

        PageDataList<ScoreLotteryRecordModel> pageDataList_ = new PageDataList<ScoreLotteryRecordModel>();
        List<ScoreLotteryRecordModel> list = new ArrayList<ScoreLotteryRecordModel>();
        pageDataList_.setPage(pageDataList.getPage());
        if (pageDataList.getList().size() > 0) {
            for (ScoreLotteryRecord scoreLotteryRecord : pageDataList.getList()) {
                ScoreLotteryRecordModel scoreLotteryRecordModel = ScoreLotteryRecordModel.instance(scoreLotteryRecord);
                scoreLotteryRecordModel.setUserName(scoreLotteryRecord.getUser().getUserName());
                list.add(scoreLotteryRecordModel);
            }
        }
        pageDataList_.setList(list);
        return pageDataList_;
    }

    @Override
    public double getScoreLotteryMoney(String year, String month) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT IFNULL(SUM(value),0) FROM ");
        sql.append(Global.DB_PREFIX).append("score_lottery_record");
        sql.append(" WHERE DATE_FORMAT(add_time,'%Y') = '").append(year).append("'");
        sql.append("   AND DATE_FORMAT(add_time,'%m') = '").append(month).append("'");
        sql.append("   AND type = ").append(ScoreLotteryModel.TYPE_MONEY);
        Query query = em.createNativeQuery(sql.toString());
        Object obj = query.getSingleResult();
        return Double.parseDouble(obj.toString());
    }
}
